﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.mainmenu {
    import flash.events.*;
    import reprise.events.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.views.components.mainmenu.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.models.world.*;

    public class DoctorsTabMediator extends Mediator implements IMediator, ITicking {

		[Inject]
        public var tabDisplay:DoctorsTabDisplay;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var gameViewModel:GameViewModel;

        override public function onRegister():void{
            this.addDoctors();
            eventMap.mapListener(this.tabDisplay, ViewCompEvent.CLICK, this.tabDisplay_click);
            eventMap.mapListener(this.tabDisplay, DisplayEvent.VISIBLE_CHANGED, this.visibleChanged);
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_DOCTOR_VIEW, this.createDoctorView);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_DOCTOR_TAB, this.disableDoctorTab);
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_DOCTOR_TAB, this.enableDoctorTab);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_DOCTOR_HUNGRY, this.onDoctorHungry);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_DOCTOR_HAS_EATEN, this.onDoctorHasEaten);
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
        }
        private function visibleChanged(_arg1:DisplayEvent):void{
            this.onTick();
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            var _local4:DoctorVO;
            if (this.tabDisplay.isHidden()){
                return;
            };
            var _local3:Array = this.worldModel.doctors;
            for each (_local4 in _local3) {
                (((_local4.hungryIn > 0)) && (this.tabDisplay.setHungryIn(_local4.id, TimeUtils.secondsToHoursMinSec(_local4.hungryIn))));
            };
        }
        private function onDoctorHungry(_arg1:InfoEvent):void{
            this.tabDisplay.showHungry(_arg1.id);
        }
        private function onDoctorHasEaten(_arg1:InfoEvent):void{
            this.tabDisplay.hideHungry(_arg1.id);
        }
        private function enableDoctorTab(_arg1:Event):void{
            eventMap.mapListener(eventDispatcher, EventConstants.ON_DOCTOR_HUNGRY, this.onDoctorHungry);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_DOCTOR_HAS_EATEN, this.onDoctorHasEaten);
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_DOCTOR_VIEW, this.createDoctorView);
        }
        private function disableDoctorTab(_arg1:Event):void{
            eventMap.unmapListener(eventDispatcher, EventConstants.ON_DOCTOR_HUNGRY, this.onDoctorHungry);
            eventMap.unmapListener(eventDispatcher, EventConstants.ON_DOCTOR_HAS_EATEN, this.onDoctorHasEaten);
            eventMap.unmapListener(eventDispatcher, EventConstants.CREATE_DOCTOR_VIEW, this.createDoctorView);
        }
        private function tabDisplay_click(_arg1:ViewCompEvent):void{
            var _local2:String = _arg1.sender;
            var _local3:IRoom = IRoom(this.worldModel.getRoomOfActor(_local2));
            var _local4:Slot = this.roomSlotsModel.getSlotById(_local3.slotId);
            if (!_local4.isWaitingSlot()){
                dispatch(new InfoEvent(EventConstants.SHOW_ROOM, String(_local4.id)));
                dispatch(new InfoEvent(EventConstants.HIGHLIGHT_ACTOR, _local2));
            };
        }
        private function createDoctorView(_arg1:InfoEvent):void{
            var _local2:DoctorVO = DoctorVO(_arg1.info);
            var _local3:IRoom = IRoom(this.worldModel.getRoomById(_local2.roomId));
            this.addDoctor(_local2.id, _local2.type, _local3.type, _local2.isHungry);
        }
        private function addDoctors():void{
            var _local3:DoctorVO;
            var _local4:IRoom;
            var _local1:Array = this.worldModel.doctors;
            var _local2:uint = _local1.length;
            if (_local2 == 0){
                return;
            };
            for each (_local3 in _local1) {
                _local4 = IRoom(this.worldModel.getRoomById(_local3.roomId));
                this.addDoctor(_local3.id, _local3.type, _local4.type, _local3.isHungry);
            };
        }
        private function addDoctor(_arg1:String, _arg2:String, _arg3:String, _arg4:Boolean):void{
            var _local5:String = (GameConstants.LANGUAGE_ROOMS_PREFIX + _arg3.toLowerCase());
            var _local6:String = this.languageModel.getTextByKey((_local5 + "_ml"));
            this.tabDisplay.addDoctor(_arg1, _arg2, _arg3, _local6, this.languageModel.getTextByKey("doctors_doctorsbox_headline"), this.languageModel.getTextByKey("doctors_doctorsbox_toolip"), this.languageModel.getTextByKey("doctors_doctorsbox_hungry"));
            if (_arg4){
                this.tabDisplay.showHungry(_arg1);
            };
        }
        override public function preRemove():void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
        }

    }
}//package net.wooga.pets.views.mainmenu 
